import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      component: () => import('../views/Mall/index.vue'),
      redirect: '/body',
      children: [
        {
          path: '/body',
          name: 'body',
          component: () => import('../views/Mall/body.vue')
        },
        {
          path: '/mall/personal',
          name: 'personal',
          component: () => import('../views/Mall/personal.vue'),
        },

        {
          path: '/mall/myorder',
          name: 'myorder',
          component: () => import('../views/Mall/myorder.vue')
        },
        {
          path: '/showdetail',
          name: 'showdetail',
          component: () => import('../views/Mall/showdetail.vue')
        },
        {
          path: '/searchGoods',
          name: 'searchGoods',
          component: () => import('../views/Mall/searchGoods.vue')
        },
        {
          path: '/mall/purchase',
          name: 'purchase',
          component: () => import('../views/Mall/purchase.vue')
        },
        {
          path: '/mall/purchase2',
          name: 'purchase2',
          component: () => import('../views/Mall/purchase2.vue')
        },
        {
          path: '/mall/pay',
          name: 'pay',
          component: () => import('../views/Mall/pay.vue')
        }, {
          path: '/mall/pay2',
          name: 'pay2',
          component: () => import('../views/Mall/pay2.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Mall/Login.vue'),

    },

    {
      path: '/register',
      name: 'register',
      component: () => import('../views/Mall/Register.vue')
    }
    ,
    {
      path: '/mall/cart',
      name: 'cart',
      component: () => import('../views/Mall/cart.vue')
    },
    {
      path: '/admin/login',
      name: 'admin',
      component: () => import('../views/Admin/Login.vue')
    },
    {
      path: '/admin/body2',
      name: 'body2',
      component: () => import('../views/Admin/Body.vue'),
      redirect: '/admin/index2',
      children: [
        {
          path: '/admin/index2',
          name: 'index2',
          component: () => import('../views/Admin/index.vue')
        },
        {
          path: '/admin/add',
          name: 'add',
          component: () => import('../views/Admin/AddGoods.vue'),
        },
        {
          path: '/admin/goods',
          name: 'goods',
          component: () => import('../views/Admin/Goods.vue'),
        },
        {
          path: '/admin/swiper',
          name: 'swiper',
          component: () => import('../views/Admin/Swiper.vue'),
        },
        {
          path: '/admin/category',
          name: 'category',
          component: () => import('../views/Admin/Category.vue'),
        },
        {
          path: '/admin/guest',
          name: 'guest',
          component: () => import('../views/Admin/Guest.vue'),
        },
        {
          path: '/admin/order',
          name: 'order',
          component: () => import('../views/Admin/Order.vue'),
        },
        {
          path: '/admin/recommend',
          name: 'recommend',
          component: () => import('../views/Admin/IndexConfig.vue')
        },
        {
          path: '/admin/new',
          name: 'new',
          component: () => import('../views/Admin/IndexConfig.vue')
        }, {
          path: '/admin/order_detail',
          name: 'order_detail',
          component: () => import('../views/Admin/OrderDetail.vue')
        },
      ]
    },

  ]
})

import { ElMessage } from 'element-plus'
/* 导航守卫 */
router.beforeEach((to, from, next) => {
  // console.log("beforeEach to:", to);
  // console.log("beforeEach from", from);
  // console.log(sessionStorage.getItem("isLogin"));
  if (to.path.startsWith('/admin') || to.path == "/register" || to.path == '/body' || to.path == '/searchGoods' || to.path == '/showdetail') {
    next();
  }
  else if (to.path != "/login" && JSON.parse(sessionStorage.getItem("isLogin")) == false) {
    /*  alert("请先登录"); */
    ElMessage.error('请先登录')
    next("/login");
  } else {
    next();
  }

});
export default router
